package com.hongkzh.www.look.view.framgent;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hongkzh.www.R;
import com.hongkzh.www.look.model.bean.CategoryBean;
import com.hongkzh.www.look.model.bean.CityAdvBean;
import com.hongkzh.www.look.model.bean.CityBean;
import com.hongkzh.www.look.model.bean.DayIntegralBean;
import com.hongkzh.www.look.model.bean.HotAdvBean;
import com.hongkzh.www.look.model.bean.StaticBean;
import com.hongkzh.www.look.enterprise.userinfo.model.bean.UserInfo;
import com.hongkzh.www.other.utils.SharedPreferencesUtil;
import com.hongkzh.www.other.utils.UIUtils;
import com.hongkzh.www.look.presenter.LKPresenter;
import com.hongkzh.www.look.view.activity.AttentionActivity;
import com.hongkzh.www.view.activity.CityAdvActivity;
import com.hongkzh.www.look.view.activity.DayIntegralActivity;
import com.hongkzh.www.look.view.activity.EarnActivity;
import com.hongkzh.www.look.enterprise.view.activity.EnterpriseActivity;
import com.hongkzh.www.look.media.view.activity.MediaActivity;
import com.hongkzh.www.view.activity.RecommendActivity;
import com.hongkzh.www.view.activity.TraditionActivity;
import com.hongkzh.www.look.enterprise.userinfo.view.activity.UserInfoActivity;
import com.hongkzh.www.look.view.activity.PublishActivity;
import com.hongkzh.www.look.view.activity.RecordActivity;
import com.hongkzh.www.look.view.activity.SearchActivity;
import com.hongkzh.www.look.view.activity.TaskActivity;
import com.hongkzh.www.look.enterprise.watch.view.activity.WatchActivity;
import com.hongkzh.www.look.view.adapter.LkRvAdapter;
import com.hongkzh.www.view.customview.MyFooter;
import com.hongkzh.www.view.fragment.BaseFragment;
import com.hongkzh.www.look.view.iview.LKIView;
import com.liaoinstan.springview.widget.SpringView;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 乐看
 */
public class LKFragment extends BaseFragment<LKPresenter> implements LKIView, LkRvAdapter.OnItemClickListener, SpringView.OnFreshListener, LkRvAdapter.OnClassifyClickListener, LkRvAdapter.OnIconClickListener, LkRvAdapter.OnTiaoZhuanClickListener {
    @BindView(R.id.lkt_left)
    TextView lktLeft;
    @BindView(R.id.lkt_left1)
    TextView lktLeft1;
    @BindView(R.id.lkt_sousuo)
    ImageView lktSousuo;
    @BindView(R.id.lktr_unselected)
    ImageView lktrUnselected;
    @BindView(R.id.lk_rv)
    RecyclerView lkRv;
    @BindView(R.id.lk_sv)
    SpringView lkSv;
    @BindView(R.id.lktr_selected)
    ImageView lktrSelected;
    @BindView(R.id.lktr_fb)
    ImageView lktrFb;
    @BindView(R.id.lktr_fbt)
    TextView lktrFbt;
    @BindView(R.id.lktr_ls)
    ImageView lktrLs;
    @BindView(R.id.lktr_lst)
    TextView lktrLst;
    @BindView(R.id.lktr_sy)
    ImageView lktrSy;
    @BindView(R.id.lktr_syt)
    TextView lktrSyt;
    @BindView(R.id.lktr_gz)
    ImageView lktrGz;
    @BindView(R.id.lktr_gzt)
    TextView lktrGzt;
    @BindView(R.id.lktr_rw)
    ImageView lktrRw;
    @BindView(R.id.lktr_rwt)
    TextView lktrRwt;
    @BindView(R.id.lkt_right)
    LinearLayout lktRight;
    Unbinder unbinder;
    private LkRvAdapter lkRvAdapter;
    private @SuppressLint("UseSparseArrays")
    Map<Integer, CityAdvBean> map = new HashMap<>();
    private MyFooter myFooter;
    private boolean lastPage;
    private UserInfo userInfo;
    @SuppressLint("UseSparseArrays")
    private Map<Integer, Class> activityMap = new HashMap<>();

    @Override
    protected int getLayoutId() {
        return R.layout.fagment_lk;
    }

    @Override
    protected void init() {
        activityMap.put(0, RecommendActivity.class);
        activityMap.put(1, EnterpriseActivity.class);
        activityMap.put(2, MediaActivity.class);
        activityMap.put(3, CityAdvActivity.class);
        activityMap.put(4, TraditionActivity.class);

        userInfo = new SharedPreferencesUtil(UIUtils.getContext()).getUserInfo();
        String loginUid = userInfo.getLoginUid();

        setPresenter(new LKPresenter());
        getPresenter().findDayIntegral(loginUid)
                .getCarouselList()
                .getCategoryList()
                .getRecommendList()
                .getStaticList()
                .getEnterpriseList()
                .getMediaList()
                .getCityAdvList()
                .getCityList()
                .getTraditionList()
                .getHotAdvList();

        myFooter = new MyFooter(getActivity());
        lkSv.setFooter(myFooter);

        lkRvAdapter = new LkRvAdapter();
        lkRv.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        lkRv.setAdapter(lkRvAdapter);
    }

    @Override
    protected void setListener() {
        lkSv.setListener(this);
        lkRvAdapter.setOnClassifyClickListener(this);
        lkRvAdapter.setOnItemClickListener(this);
        lkRvAdapter.setOnIconClickListener(this);
        lkRvAdapter.setOnTiaoZhuanClickListener(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void setDayIntegral(DayIntegralBean dayIntegralBean) {
        lktLeft.setText("" + dayIntegralBean.getData().getIntegral());
    }

    @Override
    public void setCarouselList(StaticBean staticBean) {
        lkRvAdapter.addCarouselList(staticBean);
        lkRvAdapter.notifyItemChanged(0);
    }

    @Override
    public void setCategoryList(CategoryBean categoryBean) {
        lkRvAdapter.addCategoryList(categoryBean);
        lkRvAdapter.notifyItemChanged(0);
    }

    @Override
    public void setStaticList(StaticBean staticBean) {
        lkRvAdapter.addStaticList(staticBean);
        lkRvAdapter.notifyItemChanged(0);
    }

    @Override
    public void setCityList(CityBean cityBean) {
        lkRvAdapter.addCityList(cityBean);
        lkRvAdapter.notifyItemChanged(3);
    }

    @Override
    public void setHotAdvList(HotAdvBean hotAdvBean) {
        lkRvAdapter.addHotAdvList(hotAdvBean);
        lkSv.onFinishFreshAndLoad();
    }

    @Override
    public void onLastPage(boolean lastPage) {
        this.lastPage = lastPage;
        myFooter.setLastPage(lastPage);
    }

    @Override
    public void setCityAdvList(int key, CityAdvBean cityAdvBean) {
        map.put(key, cityAdvBean);
        lkRvAdapter.addCityAdvList(map);
        lkRvAdapter.notifyItemChanged(key);
    }

    @Override
    public void onError(Exception e) {
        Log.e("https", String.valueOf(e));
    }

    @Override
    public void onRefresh() {

    }

    @Override
    public void onLoadmore() {
        if (lastPage) lkSv.onFinishFreshAndLoad();
        else getPresenter().loadmore();
    }

    @OnClick({R.id.lkt_left, R.id.lkt_left1, R.id.lkt_sousuo, R.id.lktr_unselected, R.id.lk_rv, R.id.lk_sv, R.id.lktr_selected, R.id.lktr_fb, R.id.lktr_fbt, R.id.lktr_ls, R.id.lktr_lst, R.id.lktr_sy, R.id.lktr_syt, R.id.lktr_gz, R.id.lktr_gzt, R.id.lktr_rw, R.id.lktr_rwt, R.id.lkt_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.lkt_left:
            case R.id.lkt_left1:
                startActivity(new Intent(getActivity(), DayIntegralActivity.class));
                break;
            case R.id.lkt_sousuo:
                startActivity(new Intent(getActivity(), SearchActivity.class));
                break;
            case R.id.lktr_unselected:
                lktRight.setVisibility(View.VISIBLE);
                break;
            case R.id.lktr_selected:
                lktRight.setVisibility(View.GONE);
                break;
            case R.id.lktr_fb:
            case R.id.lktr_fbt:
                startActivity(new Intent(getActivity(), PublishActivity.class));
                break;
            case R.id.lktr_ls:
            case R.id.lktr_lst:
                startActivity(new Intent(getActivity(), RecordActivity.class));
                break;
            case R.id.lktr_sy:
            case R.id.lktr_syt:
                startActivity(new Intent(getActivity(), EarnActivity.class));
                break;
            case R.id.lktr_gz:
            case R.id.lktr_gzt:
                startActivity(new Intent(getActivity(), AttentionActivity.class));
                break;
            case R.id.lktr_rw:
            case R.id.lktr_rwt:
                startActivity(new Intent(getActivity(), TaskActivity.class));
                break;
        }
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onClassifyClick(int type) {
        Toast.makeText(getActivity(), "type" + type, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onIconClick(String uid) {
        Intent intent = new Intent(getActivity(), UserInfoActivity.class);
        intent.putExtra("uid", uid);
        startActivity(intent);
    }

    @Override
    public void onItemClick(String uid, String id) {
        Intent intent = new Intent(getActivity(), WatchActivity.class);
        intent.putExtra("uid", uid);
        intent.putExtra("id", id);
        startActivity(intent);
    }

    @Override
    public void onTiaoZhuanClick(int position) {
        if (activityMap.get(position) != null)
            startActivity(new Intent(getActivity(), activityMap.get(position)));
    }
}
